<template>
	<view class="login-page">
		<view class="title-box">
			<view class="title">Hi，欢迎登录</view>
		</view>
		<view class="login-info">
			<view class="input">
				<u-input prefixIcon="account"  color="#ffffff" prefixIconStyle="font-size: 24px;color: #ffffff" placeholder="请输入您的手机号"
					:maxlength="11" shape="circle" border="none" v-model="tel" type="number">
					<template slot="suffix">
						<u-icon name="arrow-down" @click="godown" color="#ffffff" size="16"></u-icon>
					</template>
				</u-input>
			</view>
			<view class="input">
				<u-input placeholder="请输入您的密码" color="#ffffff" prefixIcon="lock" prefixIconStyle="font-size: 24px;color: #ffffff"
					shape="circle" type="password" border="none" v-model="password">
					<template slot="suffix">
						<view class="text" @click="gonavto('/pages/login/forget')">忘记密码</view>
					</template>
				</u-input>
			</view>
			<u-button style="color: #000; margin-top: 60rpx;" @click="gologin"  size="large" type="primary" color="linear-gradient(255.89deg, #FFF0E3 6.94%, #D7B39A 87.16%)"
				text="登录账号"></u-button>
			<view class="btn2" @click="gonavto('/pages/login/register')">
				<text>注册新账号</text>
			</view>
			<u-picker :show="showAccountSelect" keyName="tel" :columns="accountList" @cancel="showAccountSelect= false"
				@confirm="selectAccount"></u-picker>
		</view>
		<!-- <view class="bottom">
			<u-checkbox-group>
				<u-checkbox :checked="checked" @change="gochecked" shape="circle" active-color="#FD5674"
					size="14px"></u-checkbox>
			</u-checkbox-group>
			<view>
				登录代表同意<text>《用户协议》</text>与<text>《产品购买协议》</text>
			</view>
		</view> -->
	</view>
</template>

<script>
	import CryptoJS from 'crypto-js';
	export default {
		data() {
			return {
				tel: '',
				password: '',
				checked: true,
				showAccountSelect: false,
				historyAccounts: [], // 存储历史账号
				accountList: [] // 用于下拉选择的数据
			}
		},
		onLoad() {
			this.loadHistoryAccounts();
		},
		methods: {
			godown() {
				this.showAccountSelect = true
			},
			// 加载历史账号
			loadHistoryAccounts() {
				const accounts = uni.getStorageSync('loginHistory') || [];
				this.historyAccounts = accounts
				this.accountList.push(this.historyAccounts)
				// 如果有历史账号，自动填充最后一个
				if (accounts.length > 0) {
					const lastAccount = accounts[accounts.length - 1];
					this.tel = lastAccount.tel;
					this.password = lastAccount.password;
				}
			},
			// 选择历史账号
			selectAccount(e) {
				this.tel = e.value[0].tel;
				this.password = e.value[0].password;
				this.showAccountSelect = false
			},
			// 保存登录信息
			saveLoginInfo() {
				let accounts = uni.getStorageSync('loginHistory') || [];

				// 移除重复的账号
				accounts = accounts.filter(item => item.tel !== this.tel);

				// 添加新账号到数组末尾
				accounts.push({
					tel: this.tel,
					password: this.password
				});

				// 限制保存的账号数量（例如最多5个）
				if (accounts.length > 5) {
					accounts = accounts.slice(accounts.length - 5);
				}

				uni.setStorageSync('loginHistory', accounts);
			},
			md5Encrypt(text) {
				return CryptoJS.MD5(text).toString();
			},
			gochecked(e) {
				this.checked = e
			},
			gologin() {
				// if (this.checked == false) {
				// 	uni.showToast({
				// 		title: '请仔细阅读并同意用户协议与产品购买协议！',
				// 		icon: 'none'
				// 	})
				// 	return
				// }
				if (this.tel == '') {
					uni.showToast({
						title: '手机号不可为空！',
						icon: 'none'
					})
					return
				}
				if (this.password == '') {
					uni.showToast({
						title: '请输入密码！',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					mask: true
				})
				let params = {
					phone: this.tel,
					password: this.md5Encrypt(this.password)
				}
				this.$Request({
					method: 'POST',
					url: '/api/login/sign_in',
					data: params,
					ismsg: true,
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						// 登录成功后保存账号信息
						this.saveLoginInfo();

						uni.setStorageSync('token', res.data.token);
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 1000)
					}
				})
			},
			gonavto(url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	}
</script>
<style>
	page{
		background: linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, #000000 42.13%);
	}
</style>
<style scoped lang="scss">
	.login-page {
		height: 100vh;
		position: relative;
		background-image: url('../../static/login/login-bg.png');
		background-repeat: no-repeat;
		background-size: 100% 593px;
		.login-info {
			width: 95%;
			margin: 0 auto;
			margin-top: 80rpx;
			padding: 30rpx;

			.btn1 {
				margin-top: 60rpx;
				width: 100%;
				/* 补充单位 */
				height: 96rpx;
				/* 48px转换为rpx */
				line-height: 96rpx;
				border-radius: 10rpx;
				/* 补充单位 */
				background: linear-gradient(0deg, #FFF0E3, #FFF0E3),
				linear-gradient(255.89deg, #FFF0E3 6.94%, #D7B39A 87.16%);
				color: #000000;
				text-align: center;
			}

			.btn2 {
				margin-top: 40rpx;
				text-align: center;
				color: #FFD7A2;

				text {
					border-bottom: 1rpx solid #FFD7A2;
				}
			}

			

			.input {
				background-color: #1A1A1B;
				margin-bottom: 30rpx;
				padding: 30rpx;
				border-radius: 30rpx;
				.text {
					text-align: right;
					color: #52ECFA;
					font-size: 26rpx;
				}
			}

			.account-select {
				margin-bottom: 20rpx;
				display: flex;
				justify-content: flex-end;
			}
		}

		.title-box {
			padding-top: 260rpx;
			.title {
				font-size: 60rpx;
				font-weight: 900;
				color: #ffffff;
				text-align: center;
			}
		}

		.bottom {
			position: fixed;
			bottom: 5%;
			left: 0;
			right: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 26rpx;

			text {
				color: #FD5772;
			}
		}
	}
</style>